package com.chee.plant.server;

import org.apache.mina.common.IdleStatus;
import org.apache.mina.common.IoHandlerAdapter;
import org.apache.mina.common.IoSession;
import org.apache.mina.common.TransportType;
import org.apache.mina.transport.socket.nio.SocketSessionConfig;

public class SessionHandler extends IoHandlerAdapter {
	public void exceptionCaught(IoSession session, Throwable t) throws Exception {
		t.printStackTrace();
		session.close();
	}

	public void messageReceived(IoSession session, Object msg) throws Exception {
		String recvStr = msg.toString();
		Message recv = new Message(recvStr);
		recv.setSession(session);
		CommonData.getSessionServer().addRecvMessage(recv);
	}

	public void sessionCreated(IoSession session) throws Exception {
		if( session.getTransportType() == TransportType.SOCKET )
			((SocketSessionConfig) session.getConfig() ).setReceiveBufferSize( 2048 );
		session.setIdleTime( IdleStatus.BOTH_IDLE, 10 );
		CommonData.addRawSession(session);
	}
}
